<?php
require_once('common.php');

class role extends common{
	var
		$tbl = 'roles',
		$tblActions = 'actions',
		$tblRoleActions = 'role_actions';
	var
		$actions = array();
		
	function create_body(){}
	function update_body($no_update_fields=array()){}
	function delete_body(){
		return true;
	}
	
	
	function __construct($param=NULL) {
		parent::__construct();
		global $db;
		if (is_null($param)){
			return true;
		}elseif(is_array($param)){
			$this->f = $param;
		}elseif(is_string($param) || is_int($param)){
			$where = "";
			if (is_string($param)){
				$param = block($param);
				$where = " WHERE slug='$param' LIMIT 1";
			}elseif (is_int($param)){
				$where = " WHERE id='$param' LIMIT 1";
			}
			$sql = "
				SELECT 
					id, name, slug, description
				FROM 
					$this->tbl
				$where
			";
			$this->f = $db->query_line($sql);
			if (empty($this->f)){
				$this->event('role_not_found',"No se uncuentra la role '$param'");
			}
		}
	}

	function get_actions(){
		if (empty($this->actions)){
			global $db;
			$return = false;
			$sql = "
				SELECT
					a.id, a.slug, a.description
				FROM
					$this->tblActions as a
					INNER JOIN 
						$this->tblRoleActions as ra
					ON
						(ra.role_id = $this->id)
						AND
						(a.id = ra.action_id)
			";
			$this->actions = $db->query_assoc($sql,'slug');
			if (!empty($this->actions)){
				$return = true;
			}
		}else{
			$return = true;
		}
		return $return;
	}
	
	function has_action($action){
		$return = false;
		if (!empty($this->actions) || $this->get_actions()){
			$return = array_key_exists($action,$this->actions);
		}
		return $return;
	}
}
?>